package com.cat.chapter049;

/**
 * @author cat
 * @description https://leetcode.cn/problems/gas-station/
 * @create 2025/7/19 14:40
 * @since JDK17
 */

public class Solution04 {
    public int canCompleteCircuit(int[] gas, int[] cost) {
        int[] arr = new int[gas.length * 2];
        int n = arr.length, m = gas.length, sum = 0;
        for (int i = 0; i < n; i++) {   // 制作一个扩容的消耗表
            arr[i] = gas[i % m] - cost[i % m];
        }
        for (int l = 0, r = 0; r < n; r++) {    //
            sum += arr[r];
            if (sum < 0) {  //
                l++;
                sum = 0;
            }
            if (r - l == m) {
                return l;
            }
        }

        return -1;
    }
}
